Exercises: Functions

Exercises about functions

1. Create a simple Lua function that receives five parameters and returns the result of the average of these five values.

2. Create a Lua function called student_passed() that also receives five parameters representing the five marks scored by a student in a quiz. The function should calculate the average of all marks and return true if the student passed (average greater or equal to 70) or false if the student failed (average less than 70).

3. Create a Lua function that returns if a number is inside a given range. The function should receive three parameters. For example, is_value_inside_range(5, 2, 12) should return true, since 5 is between 2 and 12 (or false otherwise).

4. Create a simple Lua function that receives a temperature value in Celsius as a parameter and returns the value converted to Fahrenheit. The conversion formula is listed below.

5. Create a simple Lua function that receives as a parameter the values of two rectangles (x1, y1, width1, height1, x2, y2, width2, height2) and returns true or false if the two rectangles are colliding with each other or not. This simple collision test falls under what we call AABB (axis-align bounding box). Think of what are the conditions that need to be true for us to safely tell that the first rectangle is colliding with the second one.


Solutions: solutions-functions.zip